package com.ktwlsoft.archivemanage.domain.business.repositoryService.mend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ktwlsoft.archivemanage.domain.dao.entity.Account;
import com.ktwlsoft.archivemanage.domain.dao.entity.mend.MendListOptLog;
import com.ktwlsoft.archivemanage.domain.dao.repository.mend.MendListOptLogMapper;
import com.ktwlsoft.archivemanage.service.security.UserPrincipal;
import com.ktwlsoft.archivemanage.service.utils.SnowFlake;
import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 整改单操作日志
 */
@Service
public class MendListOptLogRepositoryService {

    Logger logger = LoggerFactory.getLogger(MendListOptLogRepositoryService.class);

    @Getter
    private final MendListOptLogMapper mendListOptLogMapper;

    public MendListOptLogRepositoryService(MendListOptLogMapper mendListOptLogMapper) {
        this.mendListOptLogMapper = mendListOptLogMapper;
    }

    public int insert(Long mendId, MendListOptLog.OperationType operationType){
        Account account = UserPrincipal.getCurrentUser();
        if(account==null) {
            logger.error("写整改单操作历史时，未获取到当前用户！");
            return 0;
        }
        return mendListOptLogMapper.insert(new MendListOptLog(
                SnowFlake.getNextId(),
                mendId,
                new Date(),
                account.getRealName(),
                account.getId(),
                operationType
        ));
    }

    public PageInfo<MendListOptLog> findByPage(int pn, int pageSize, long mendId){
        PageHelper.startPage(pn, pageSize);
        return new PageInfo<>(mendListOptLogMapper.selectByMendId(mendId));
    }
}
